<?php

/**
 * 自动生成 MerUserPromoteStatService
 * 
 * @filepath  Apps/Common/Service/MerUserPromoteStatService.class.php
 * @author  gt_admin 
 * @version v1.0 , 2015-09-23
 */

namespace Common\Service;

class MerUserPromoteStatService {

  private static $instance;

  public static function instance() {
    if (self::$instance == null) {
      $instance = new MerUserPromoteStatService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'id' => '',
      'date' => '',
      'user_id' => '',
      'sub_num' => '0',
    );
  }

  function get_by_id($id) {
    $MerUserPromoteStat = M('MerUserPromoteStat');
    $data = $MerUserPromoteStat->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'user_id' => '' ,
      'start_time' => '',
      'end_time' => '',
      'page' => 1,
      'page_size' => 6,
      'status' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $MerUserPromoteStat = M('MerUserPromoteStat');

    $where = array();

    if (!empty($config['status'])) {
      $where['status'] = $config['status'];
    }

    if (!empty($config['user_id'])) {
      $where['user_id'] = $config['user_id'];
    }
    
    if ( !empty( $config['start_time'] ) ) {
      $where['date'][] = array('egt' , $config['start_time']);
    }
    
    if ( !empty( $config['end_time'] ) ) {
      $where['date'][] = array('elt' , $config['end_time']);
    }    
    
    
    if ($config['count']) {
      return $MerUserPromoteStat->where($where)->count();
    } else {
      $order = $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $MerUserPromoteStat
        ->where($where)
        ->limit($limit)->order($order)->select();
      //echo $MerUserPromoteStat->_sql();
      return $data ? $data : array();
    }
  }
  
  function count_by_config( $config ) {
    
    
    $where['user_id'] = $config['user_id'];
    if ( !empty( $config['start_time'] ) ) {
      $where['date'][] = array('egt' , $config['start_time']);
    }
    if ( !empty( $config['end_time'] ) ) {
      $where['date'][] = array('elt' , $config['end_time']);
    }
    
    $MerUserPromoteStat = M('MerUserPromoteStat');
    $total = $MerUserPromoteStat->where($where )->sum('sub_num');
    //echo $MerUserPromoteStat->_sql();
    return $total ;
  }
    

  function create($data) {
    $MerUserPromoteStat = M('MerUserPromoteStat');
    $ret = $MerUserPromoteStat->add($data);
    if ($ret === FALSE) {
      return ajax_arr('添加失败', FALSE);
    } else {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    }
  }

  function update($id, $data) {
    $MerUserPromoteStat = M('MerUserPromoteStat');

    $ret = $MerUserPromoteStat->where("id = %d", $id)->save($data);
    if ($ret === FALSE) {
      return ajax_arr('编辑失败', FALSE);
    } else {
      return ajax_arr('编辑成功', TRUE);
    }
  }

  function delete($ids) {
    $MerUserPromoteStat = M('MerUserPromoteStat');
    $ret = $MerUserPromoteStat->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

}
